home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / Direct Pixel Access / Source / Direct Pixel Access.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  6.6 KB  |  252 lines  |  [TEXT/CWIE]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    Direct Pixel Access                                        */
  4. /*                                                                            */
  5. /*    Description:    This snippet shows one example of how to directly        */
  6. /*                    change the pixel values stored in a pixel image.        */
  7. /*                    The original pixel image is obtained from a 'icl8'        */
  8. /*                    resource.  Only the first 20 columns of the first        */
  9. /*                    20 rows of the 'icl8' image is used.                    */
  10. /*                                                                            */
  11. /*    Files:            Direct Pixel Access.π                                    */
  12. /*                    Direct Pixel Access.π.rsrc                                */
  13. /*                    Direct Pixel Access.c                                    */
  14. /*                                                                            */
  15. /*    Programmer:        Edgar Lee                                                */
  16. /*    Organization:    Apple Computer, Inc.                                    */
  17. /*    Department:        Developer Technical Support, DTS                        */
  18. /*    Language:        C (Think C version 5.0.2)                                */
  19. /*    Date Created:    03-06-92                                                */
  20. /*                                                                            */
  21. /****************************************************************************/
  22.  
  23. #include <Dialogs.h>
  24. #include <Fonts.h>
  25. #include <Resources.h>
  26. #include <QDOffscreen.h>
  27. #include <TextUtils.h>
  28. /* Constant Declarations */
  29.  
  30. #define    WWIDTH    600
  31. #define    WHEIGHT    400
  32.  
  33. #define WLEFT    (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  34. #define WTOP    (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  35.  
  36. /* Global Variable Definitions */
  37.  
  38. WindowPtr        gWindow;
  39. PixMapHandle    gPixmap;
  40.  
  41. void initMac();
  42.  
  43. void createWindow();
  44. void createOffscreen();
  45. void drawPixelImageData();
  46. void doEventLoop();
  47.  
  48. void main(void)
  49. {
  50.     initMac();
  51.     
  52.     createWindow();
  53.     createOffscreen();
  54.     
  55.     doEventLoop();
  56. }
  57.  
  58. void initMac()
  59. {
  60.     MaxApplZone();
  61.  
  62.     InitGraf( &qd.thePort );
  63.     InitFonts();
  64.     InitWindows();
  65.     InitMenus();
  66.     TEInit();
  67.     InitDialogs( nil );
  68.     InitCursor();
  69.     FlushEvents( 0, everyEvent );
  70. }
  71.  
  72. void createWindow()
  73. {
  74.     Rect        rect;
  75.  
  76.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  77.     gWindow = NewCWindow( 0L, &rect, "\pDirect Pixel Access", true, documentProc,
  78.                             (WindowPtr)-1L, true, 0L );                        
  79.     SetPort( gWindow );
  80.     
  81.     TextFont( geneva );
  82.     TextSize( 9 );
  83.     TextMode( srcXor );
  84. }
  85.  
  86. void createOffscreen()
  87. {
  88.     Rect        rect;
  89.     Handle        iclHandle;
  90.     
  91.     SetRect( &rect, 0, 0, 32, 32 );
  92.     
  93.     /* Create offscreen pixmap image using an 'icl8' icon resource. */
  94.  
  95.     iclHandle = GetResource( 'icl8', 129 );
  96.     HLock( iclHandle );
  97.     HNoPurge( iclHandle );
  98.     
  99.     gPixmap = (PixMapHandle)NewHandle( sizeof( PixMap ) );
  100.     
  101.     (**gPixmap).baseAddr = *iclHandle;
  102.     (**gPixmap).rowBytes = ((32 * 8) / 8) | 0x8000;
  103.     (**gPixmap).bounds = rect;
  104.     (**gPixmap).pmVersion = 0;
  105.     (**gPixmap).packType = 0;
  106.     (**gPixmap).packSize = 0;
  107.     (**gPixmap).hRes = 72;
  108.     (**gPixmap).vRes = 72;
  109.     (**gPixmap).pixelSize = 8;
  110.     (**gPixmap).planeBytes = 0;
  111.     (**gPixmap).pmReserved = 0;
  112.     (**gPixmap).pixelType = 0;
  113.     (**gPixmap).cmpCount = 1;
  114.     (**gPixmap).cmpSize = 8;
  115.     (**gPixmap).pmTable = GetCTable( 8 );
  116.     
  117.     /* Give a unique seed for the pixmap's colortable. */
  118.     (**(**gPixmap).pmTable).ctSeed = GetCTSeed();
  119. }
  120.  
  121. void drawPixelImageData()
  122. {
  123.     int                row, col;
  124.     Rect            rect;
  125.     unsigned char    value;
  126.     char            *image;
  127.     int                index = 0;
  128.     Str255            string;
  129.     RGBColor        color = { 32000, 32000, 32000 };
  130.     Byte            mode;
  131.     
  132.     ForeColor( blackColor );
  133.     
  134.     /* For this example, let's just use only the upper left corner of the image. */
  135.     SetRect( &rect, 0, 0, 20, 20 );
  136.     
  137.     /* Set the pointer to the beginning of the pixel image. */
  138.     image = GetPixBaseAddr( gPixmap );
  139.     
  140.     /*********************************************************************************/
  141.     /* Switch into 32-bit addressing mode before accessing the pixel image directly. */
  142.     /*********************************************************************************/
  143.  
  144.     mode = true32b;
  145.     SwapMMUMode( (SInt8*)&mode );
  146.     
  147.     /*****************************************************************/
  148.     /* For this example, let's set the pixel values of the left half */
  149.     /*   of the image to half their original values.                 */
  150.     /*****************************************************************/
  151.     
  152.     for (row = 0; row < rect.bottom; row++)
  153.     {
  154.         /* Loop through the first 10 columns of the pixel image. */
  155.         for (index = 0, col = 0; col < rect.right / 2; col++)
  156.         {
  157.             /* Set the value at this index to half its value. */
  158.             value = (unsigned char)*(image + index);
  159.             *(image + index) = value / 2;
  160.             
  161.             index++;
  162.         }
  163.         
  164.         /* Increment the pointer to the next row of the pixel image. */
  165.         image += ((**gPixmap).rowBytes & 0x7fff);
  166.     }
  167.     
  168.     /********************************************************************/
  169.     /* Switch back to 24-bit addressing when done accessing the pixels. */
  170.     /********************************************************************/
  171.  
  172.     SwapMMUMode( (SInt8*)&mode );
  173.     
  174.     /* Draw the offscreen image to the screen to see what it looks like. */
  175.     CopyBits( (BitMap *)*gPixmap, &gWindow->portBits, &rect,
  176.             &gWindow->portRect, srcCopy, 0 );
  177.     
  178.     RGBForeColor( &color );
  179.     
  180.     /* Again, set the pointer to the beginning of the pixel image. */
  181.     image = GetPixBaseAddr( gPixmap );
  182.     
  183.     /***************************************************************/
  184.     /* Finally let's display the pixel values on top of the image. */
  185.     /***************************************************************/
  186.     
  187.     /* Loop through the first 20 rows of the pixel image. */
  188.     for (row = 0; row < rect.bottom; row++)
  189.     {
  190.         /* Loop through the first 20 columns of the pixel image. */
  191.         for (index = 0, col = 0; col < rect.right; col++)
  192.         {
  193.             /* Get the value at this index into the pixel image. */
  194.             value = (unsigned char)*(image + index);
  195.             
  196.             MoveTo( col * 30, row * 20 );
  197.             LineTo( col * 30, (row + 1) * 20 );
  198.             LineTo( (col + 1) * 30, (row + 1) * 20 );
  199.             
  200.             MoveTo( (col * 30) + 6, (row * 20) + 14 );
  201.             NumToString( (long)value, string );
  202.             DrawString( string );
  203.             
  204.             index++;
  205.         }
  206.         
  207.         /* Increment the pointer to the next row of the pixel image. */
  208.         image += ((**gPixmap).rowBytes & 0x7fff);
  209.     }
  210. }
  211.  
  212. void doEventLoop()
  213. {
  214.     EventRecord event;
  215.     WindowPtr   window;
  216.     short       clickArea;
  217.     Rect        screenRect;
  218.  
  219.     for (;;)
  220.     {
  221.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  222.         {
  223.             if (event.what == mouseDown)
  224.             {
  225.                 clickArea = FindWindow( event.where, &window );
  226.                 
  227.                 if (clickArea == inDrag)
  228.                 {
  229.                     screenRect = (**GetGrayRgn ()).rgnBBox;
  230.                     DragWindow( window, event.where, &screenRect );
  231.                 }
  232.                 else if (clickArea == inContent)
  233.                 {
  234.                     if (window != FrontWindow())
  235.                         SelectWindow( window );
  236.                 }
  237.                 else if (clickArea == inGoAway)
  238.                     if (TrackGoAway( window, event.where ))
  239.                         return;
  240.             }
  241.             else if (event.what == updateEvt)
  242.             {
  243.                 window = (WindowPtr)event.message;    
  244.                 SetPort( window );
  245.                 
  246.                 BeginUpdate( window );
  247.                 drawPixelImageData();
  248.                 EndUpdate( window );
  249.             }
  250.         }
  251.     }
  252. }